304B - Calendar - CodeForces Solution


brute force implementation *1300

Please click on ads to support us..

Python Code:

import datetime
t1=datetime.datetime.strptime(input(),'%Y:%m:%d')
t2=datetime.datetime.strptime(input(),'%Y:%m:%d')
print(abs((t2-t1).days))

C++ Code:

#include<bits/stdc++.h>
#include<iomanip>
#include<string>
#include <vector>
#include<algorithm>
#include <cmath>
#include<cstring>
using namespace std;
#define ll long long
#define gcd(a,b)  __gcd(a,b)
#define MOD ll(1e9+7)
#define ld long double
#define rep(i,x,y) for(ll i=x;i<y;i++)
#define rrep(i,x,y) for(ll i=x;i>=y;i--)
#define max2(a,b) ((a<b)?b:a)
#define max3(a,b,c) max2(max2(a,b),c)
#define min2(a,b) ((a>b)?b:a)
#define min3(a,b,c) min2(min2(a,b),c)
#define fast  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define cy  cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define pb push_back
#define mll map<ll,ll>
#define pii pair<ll,ll>
void solve()
{ ll d=0;
  string s1,s2;
    cin>>s1>>s2;
    int n=10;
    n = s1.length();
    string a1,a2,a3,b1,b2,b3;
    rep(i,0,4)
    {
        a1 += s1[i];
    }
    rep(i,5,7)
    {
        a2 += s1[i];
    }
    rep(i,8,10)
    {a3 += s1[i];}
    int y1 = stoi(a1);
    int m1 = stoi(a2);
    int d1  = stoi(a3);
    rep(i,0,4)
    {
        b1 += s2[i];
    }
    rep(i,5,7)
    {
        b2 += s2[i];
    }
     rep(i,8,10)
    {b3 += s2[i];}
    int y2 = stoi(b1);
    int m2 = stoi(b2);
    int d2 = stoi(b3); 
    if(y1==y2)
    {   ll days =0;
        if(m1==m2)
        {
            days = abs(d2-d1);
            cout<<days;
        }


        else{
          if(m1>m2)
          {swap(m1,m2);
          swap(d1,d2); }           
        for(int i=m1+1;i<m2;++i)
        {
            if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
         {
            days += 31;
 
         }
         else if(i==4 || i==6 || i==9 || i==11)
            {days += 30;}
        else if(i==2)
        {
            if(y2%4==0)
                {days += 29;}
            else
                {days += 28;}
        }
        }
        if(m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12)
         {
            days += 31-d1;
 
         }
         else if(m1==4 || m1==6 || m1==9 || m1==11)
            {days += 30-d1;}
        else if(m1==2)
        {
            if(y2%4==0)
                {days += 29-d1;}
            else
                {days += 28-d1;}}
             
            days += d2-1;
            cout<<days+1<<endl;
             
            


    }
    }
else{
    if(y1>y2)
    {
        swap(y1,y2);
        swap(d1,d2);
        swap(m1,m2);
    }
    ll days=0;
     
    for(int i = y1+1;i<=y2-1;++i)
    {
        if(i%4==0)
            {days+= 366;}
        else
            { days += 365;}
    }
     
    
    for(int i=1;i<m2;++i)
    {
         if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
         {
            days += 31;
 
         }
         else if(i==4 || i==6 || i==9 || i==11)
            {days += 30;}
        else if(i==2)
        {
            if(y2%4==0)
                {days += 29;}
            else
                {days += 28;}
        }
    }
    days += d2-1;
     
 
    for(int i=1;i<m1;++i)
    {
        if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
         {
            d += 31;
 
         }
         else if( i==4 || i==6 || i==9 || i==11)
            {d += 30;}
       
        else if(i==2)
        {
            if(y1%4==0 && y1!=1900)
                {d+= 29;}
            else
                {d += 28;}
        }
         
    }
 
    d += d1;
     if(y1%4!=0 || y1==1900)
    {days += 365-d;}
else
{
         days += 366-d;
}
    cout<<days+1<<endl;}

    
     
 
 
   
      
     
}
int main()
 {     fast;
 ll t=1;
 //cin>>t;
 while(t--)
 { solve();
        
 }
 return 0;}


Comments

Submit
0 Comments
More Questions

1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number